/**
 * 
 */
package com.android.gtdroid;


import android.app.SearchManager;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.Toast;

/**
 * @author xiang
 *
 */
public class MainAct extends TabActivity implements OnCheckedChangeListener {
	
	private final String TAG = "MainAct";
	private final int bgColor = 0xff444444;
	private final int hlColor = 0xff666666;
	
	private TabHost mTabHost;
	private RadioButton rbCurrent;
//	private boolean useTitleFeature = false;
//	private LayoutInflater li;
	private GTDApp mGA;
	
	private Intent ittList;
	private Intent ittContext;
	private Intent ittPosition;
	private Intent ittSearch;
	private Intent ittMore;
	
	private RadioButton rbList;
	private RadioButton rbContext;
	private RadioButton rbPosition;
	private RadioButton rbSearch;
	private RadioButton rbMore;
	
	private View vList;
	private View vContext;
	private View vPosition;
	private View vSearch;
	private View vMore;
	private View curTitleView;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Log.e(TAG, "onCreate is called!");
		
//		useTitleFeature = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);		// true
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.main);
		
		initTitleView();
		
//		li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		mGA = (GTDApp) getApplication();
		
		this.ittList = new Intent().setClass(this, ListAct.class);
		this.ittContext = new Intent().setClass(this, ContextAct.class);
		this.ittPosition = new Intent().setClass(this, PositionAct.class);
		this.ittSearch = new Intent().setClass(this, SearchAct.class);
		this.ittMore = new Intent().setClass(this, MoreAct.class);
		
		rbList = (RadioButton) findViewById(R.id.rb_list);
		rbContext = (RadioButton) findViewById(R.id.rb_context);
		rbPosition = (RadioButton) findViewById(R.id.rb_position);
		rbSearch = (RadioButton) findViewById(R.id.rb_search);
		rbMore = (RadioButton) findViewById(R.id.rb_more);
		
		initRadios();
		
		setupIntent();
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		
		Log.e(TAG, "onStart is called!");
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		
		Log.e(TAG, "onStop is called!");
	}
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		
		Log.e(TAG, "onRestart is called!");
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		Log.e(TAG, "onResume is called!");
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		Log.e(TAG, "onPause is called!");
		
//		handleSearchQuery(getIntent());		// when press search button, the original activity is paused.
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		Log.e(TAG, "onDestroy is called!");
	}
	
	private void initTitleView() {
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_main);
		vList = findViewById(R.id.ll_main);
		curTitleView = vList;
//		vList.setVisibility(View.INVISIBLE);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_context);
		vContext = findViewById(R.id.ll_context);
		vContext.setVisibility(View.GONE);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_pos);
		vPosition = findViewById(R.id.ll_pos);
		vPosition.setVisibility(View.GONE);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_search);
		vSearch = findViewById(R.id.ll_search);
		vSearch.setVisibility(View.GONE);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_more);
		vMore = findViewById(R.id.ll_more);
		vMore.setVisibility(View.GONE);
	}
	
	
	private void initRadios() {
		rbList.setOnCheckedChangeListener(this);
		rbContext.setOnCheckedChangeListener(this);
		rbPosition.setOnCheckedChangeListener(this);
		rbSearch.setOnCheckedChangeListener(this);
		rbMore.setOnCheckedChangeListener(this);
		
		rbCurrent = rbList;
		rbCurrent.setBackgroundColor(hlColor);
		
	}
	
	private void setupIntent() {
		this.mTabHost = getTabHost(); // The activity TabHost

		mTabHost.addTab(mTabHost.newTabSpec("list").setIndicator("�б�", null)
				.setContent(ittList));
		mTabHost.addTab(mTabHost.newTabSpec("context").setIndicator("�龳", null)
				.setContent(ittContext));
		mTabHost.addTab(mTabHost.newTabSpec("position").setIndicator("λ��", null)
				.setContent(ittPosition));
		mTabHost.addTab(mTabHost.newTabSpec("search").setIndicator("����", null)
				.setContent(ittSearch));
		mTabHost.addTab(mTabHost.newTabSpec("more").setIndicator("���", null)
				.setContent(ittMore));
		mTabHost.setCurrentTab(0);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
//		return super.onCreateOptionsMenu(menu);
		boolean result = super.onCreateOptionsMenu(menu);
		
		menu.add(0, C.MENU.NEW_TASK, Menu.NONE, getString(R.string.menu_new_task))
			.setIcon(android.R.drawable.btn_plus);
		menu.add(0, C.MENU.COMPLETED, Menu.NONE, getString(R.string.menu_completed))
			.setIcon(android.R.drawable.ic_dialog_info);
		menu.add(0, C.MENU.TRASH, Menu.NONE, getString(R.string.menu_trash))
			.setIcon(android.R.drawable.btn_star);
		menu.add(0, C.MENU.SEARCH, Menu.NONE, getString(R.string.menu_search)).setIcon(
				android.R.drawable.ic_menu_search);
		
		return result;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Intent itt = null;
		// int index = myListView.getSelectedItemPosition();
		switch (item.getItemId()) {
			case C.MENU.NEW_TASK:
				itt = new Intent(MainAct.this, NewTaskAct.class);
				startActivityForResult(itt, 0);
				
				return true;
				
			case C.MENU.COMPLETED: // completed task
				// setTitle("edit list");
				// Cursor csr =
				// ga.getDbta().fetchTasks(DBTaskAdapter.KEY_COMPLETED+"="+TaskItem.COMPLETED);
				itt = new Intent(MainAct.this, ViewTaskAct.class);
	
				itt.putExtra("list_index", -1);
				itt.putExtra("task_status", C.TASK.COMPLETED);
	
				startActivity(itt);
	
				return true;
	
			case C.MENU.TRASH: // deleted task
				// setTitle("remove list");
				itt = new Intent(MainAct.this, ViewTaskAct.class);
	
				itt.putExtra("list_index", -1);
				itt.putExtra("task_status", C.TASK.CANCELLED);
	
				startActivity(itt);
	
				return true;
				
			case C.MENU.SEARCH:
				onSearchRequested();
				
//				itt = getIntent();
//				itt.setClass(MainAct.this, SearchAct.class);
//				itt = new Intent(MainAct.this, SearchAct.class);
//				
//				startActivity(itt);
				return true;
	
			default:
				break;
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	@Override
    public boolean onKeyUp(int keyCode, KeyEvent event)
    {
        /*if (keyCode == KeyEvent.KEYCODE_SEARCH)
            Toast.makeText(this, "search button pressed", Toast.LENGTH_LONG).show();*/
            
        // TODO Auto-generated method stub
        return super.onKeyUp(keyCode, event);
    }
	
	/*@Override
	public boolean onSearchRequested() {
		// TODO Auto-generated method stub
		Toast.makeText(this, "MainAct: hit search", Toast.LENGTH_LONG).show();
//		Log.e(TAG, "on SearchRequested called!");
		
		return super.onSearchRequested();
	}*/
	
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if (isChecked) {
			switch (buttonView.getId()) {
				case R.id.rb_list:
					this.mTabHost.setCurrentTabByTag("list");
					
					rbList.setBackgroundColor(hlColor);
					rbCurrent.setBackgroundColor(bgColor);
					rbCurrent = rbList;
					
					curTitleView.setVisibility(View.INVISIBLE);
					vList.setVisibility(View.VISIBLE);
					curTitleView = vList;
					/*if (useTitleFeature) {
//						this.requestWindowFeature(Window.FEATURE_NO_TITLE);
						
						getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_main);
						curTitleId = R.id.ll_list;
					}*/
					break;
					
				case R.id.rb_context:
					this.mTabHost.setCurrentTabByTag("context");
					
					rbContext.setBackgroundColor(hlColor);
					rbCurrent.setBackgroundColor(bgColor);
					rbCurrent = rbContext;
					
					curTitleView.setVisibility(View.INVISIBLE);
					vContext.setVisibility(View.VISIBLE);
					curTitleView = vContext;
					/*if (useTitleFeature) {
//						getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_cmn);
						v = mySetFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_cmn);
						v.setVisibility(View.INVISIBLE);
//						curTitleId = R.id.ll_cmn;
					}*/
//					View v = findViewById(curTitleId);
					break;
					
				case R.id.rb_position:
					this.mTabHost.setCurrentTabByTag("position");
					
					rbPosition.setBackgroundColor(hlColor);
					rbCurrent.setBackgroundColor(bgColor);
					rbCurrent = rbPosition;
					
					curTitleView.setVisibility(View.INVISIBLE);
					vPosition.setVisibility(View.VISIBLE);
					curTitleView = vPosition;
					/*if (useTitleFeature)
						getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_task);*/
					break;
					
				case R.id.rb_search:
					this.mTabHost.setCurrentTabByTag("search");
					
					rbSearch.setBackgroundColor(hlColor);
					rbCurrent.setBackgroundColor(bgColor);
					rbCurrent = rbSearch;
					
					curTitleView.setVisibility(View.INVISIBLE);
					vSearch.setVisibility(View.VISIBLE);
					curTitleView = vSearch;
					break;
					
				case R.id.rb_more:
					this.mTabHost.setCurrentTabByTag("more");
					
					rbMore.setBackgroundColor(hlColor);
					rbCurrent.setBackgroundColor(bgColor);
					rbCurrent = rbMore;
					
					curTitleView.setVisibility(View.INVISIBLE);
					vMore.setVisibility(View.VISIBLE);
					curTitleView = vMore;
					break;
					
				default:
					break;
			}
		}
	}
	
}
